﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DataAccessLayer
{
    public partial class Person
    {
        public static Person FindByPersonID_IncludeDeleted(linqDatabaseDataContext db, int PersonID)
        {
            var subquery = (from u in db.Persons where u.ID == (from v in db.Persons where v.PersonID == u.PersonID orderby v.Timestamp descending select v).FirstOrDefault().ID select u);
            return ((from u in subquery where u.PersonID == PersonID select u).SingleOrDefault());
        }

        public static Person FindByPersonID(linqDatabaseDataContext db, int PersonID)
        {
            var subquery = (from u in db.Persons where u.ID == (from v in db.Persons where v.PersonID == u.PersonID orderby v.Timestamp descending select v).FirstOrDefault().ID select u);
            return ((from u in subquery where !u.IsDeleted && u.PersonID == PersonID select u).SingleOrDefault());
        }

        public static Person FindByNationalNumber_IncludeDeleted(linqDatabaseDataContext db, string NationalNumber)
        {
            var subquery = (from u in db.Persons where u.ID == (from v in db.Persons where v.PersonID == u.PersonID orderby v.Timestamp descending select v).FirstOrDefault().ID select u);
            return ((from u in subquery where u.NationalNumber == NationalNumber select u).SingleOrDefault());
        }

        public static Person FindByNationalNumber(linqDatabaseDataContext db, string NationalNumber)
        {
            var subquery = (from u in db.Persons where u.ID == (from v in db.Persons where v.PersonID == u.PersonID orderby v.Timestamp descending select v).FirstOrDefault().ID select u);
            return ((from u in subquery where !u.IsDeleted && u.NationalNumber == NationalNumber select u).SingleOrDefault());
        }

        public static List<Person> ToList_IncludeDeleted(linqDatabaseDataContext db)
        {
            var subquery = (from u in db.Persons where u.ID == (from v in db.Persons where v.PersonID == u.PersonID orderby v.Timestamp descending select v).FirstOrDefault().ID select u);
            return ((from u in subquery select u).ToList());
        }

        public static List<Person> ToList_IncludeDeleted_Filter(linqDatabaseDataContext db, string FirstName, string LastName, string NationalNumber)
        {
            var subquery = (from u in db.Persons where u.ID == (from v in db.Persons where v.PersonID == u.PersonID orderby v.Timestamp descending select v).FirstOrDefault().ID select u);
            return ((from u in subquery where u.FirstName.Contains(FirstName) && u.LastName.Contains(LastName) && u.NationalNumber.Contains(NationalNumber) select u).ToList());
        }

        public static List<Person> ToList(linqDatabaseDataContext db)
        {
            var subquery = (from u in db.Persons where u.ID == (from v in db.Persons where v.PersonID == u.PersonID orderby v.Timestamp descending select v).FirstOrDefault().ID select u);
            return ((from u in subquery where !u.IsDeleted select u).ToList());
        }

        public static List<Person> ToList_Filter(linqDatabaseDataContext db, string FirstName, string LastName, string NationalNumber)
        {
            var subquery = (from u in db.Persons where u.ID == (from v in db.Persons where v.PersonID == u.PersonID orderby v.Timestamp descending select v).FirstOrDefault().ID select u);
            return ((from u in subquery where !u.IsDeleted && u.FirstName.Contains(FirstName) && u.LastName.Contains(LastName) && u.NationalNumber.Contains(NationalNumber) select u).ToList());
        }

        public static List<Person> HistoryToListByPersonID(linqDatabaseDataContext db, int PersonID)
        {
            return (from u in db.Persons where u.PersonID == PersonID orderby u.Timestamp ascending select u).ToList();
        }
    }
}
